package com.qf.service.impl;

import com.qf.entity.*;
import com.qf.mapper.ClassroomMapper;
import com.qf.mapper.StudentMapper;
import com.qf.mapper.SubjectMapper;
import com.qf.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Override
    public Student findById(int id) {
        return studentMapper.findById(id);
    }

    @Override
    public List<Student> findAll() {
        return studentMapper.findAll();
    }

    @Override
    public List<Student> findByKeyWord(HashMap<String, Object> map) {
        return studentMapper.findByKeyWord(map);
    }

    @Override
    public void add(Student student) {
        studentMapper.add(student);
    }

    @Override
    public void edit(Student student) {
        studentMapper.edit(student);
    }

    @Override
    public void deleteById(int id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroom() {
        return studentMapper.findAllStudentAndClassroom();
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuid(int id) {
        return studentMapper.findStudentAndAllSubjectByStuid(id);
    }

    @Override
    public StudentVO findStudentAndAllSubjectAndTeacherByStuid(int id) {
        return studentMapper.findStudentAndAllSubjectAndTeacherByStuid(id);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroomV2() {
        //最终结果，存储学生信息和教室信息
        List<StudentVO> list =new ArrayList<>();
        //查询所有学生信息
       List<Student>studentList= studentMapper.findAll();
       //遍历得到所有学生
        studentList.forEach(student -> {
            //根据cid查对应教室
          Classroom classroom= classroomMapper.findClassroomById(student.getCid());
          //创建封装对象
            StudentVO studentVO=new StudentVO();
            //将student中的属性赋值到studentvo中
            BeanUtils.copyProperties(student,studentVO);
            //将查到的教室信息放入studentvo对象
            studentVO.setClassroom(classroom);
            list.add(studentVO);

        });
        return list;

    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuidV2(int id) {
        //查一个学生
        Student student=studentMapper.findById(id);
        //准备list存储这个学生的每个学科对象
        List<Subject> subjectList=new ArrayList<>();
        //根据学生id查询关联表中所有的学科id
        List<Integer> subIdList=studentMapper.findAllSubjectId(id);
        //遍历所有学科id
        subIdList.forEach(subId->{
            //根据id获得每个学科对象
            Subject subject=subjectMapper.findSubjectById(subId);
            subjectList.add(subject);
        });
        //封装到studentvo中
        StudentVO studentVO=new StudentVO();
        BeanUtils.copyProperties(student,studentVO);
        studentVO.setSubjectListV2(subjectList);
        return studentVO;
    }

    @Override
    public Student login(String username) {
        return studentMapper.login(username);
    }
}
